//
//  PlayerViewController.m
//  HDMusicDemo
//
//  Created by Lida on 2017/8/14.
//  Copyright © 2017年 Lida. All rights reserved.
//

#import "PlayerViewController.h"
#import "HD_LiDa_PlayerTool.h"
#import "HD_LiDa_Playermanager.h"
#import "MusicInfo.h"
#import "HD_LiDa_LrcTable.h"
#import <MediaPlayer/MediaPlayer.h>

@interface PlayerViewController ()

@property (weak, nonatomic) IBOutlet UILabel *currentTime;
@property (weak, nonatomic) IBOutlet UILabel *allTime;
@property (weak, nonatomic) IBOutlet UIView *lrcView;
@property (weak, nonatomic) IBOutlet UISlider *progressSlider;

@property (strong, nonatomic) HD_LiDa_LrcTable *lrcTableView; //歌词Table
@property (strong, nonatomic) NSTimer *timer;
@end

@implementation PlayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    [self initViews];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
   
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startPlay) name:ShouldPlay object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startPause) name:ShouldPause object:nil];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(upDateUI) userInfo:nil repeats:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
   
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[HD_LiDa_PlayerTool sharedHD_LiDa_PlayerTool]stopAudio];
}

- (void)initViews
{
    //歌词view
    self.lrcTableView = [[HD_LiDa_LrcTable alloc] initWithFrame:_lrcView.bounds style:UITableViewStylePlain];
    [self.lrcView addSubview:self.lrcTableView];
}

- (void)startPlay
{
    NSLog(@"开始播放");
}

- (void)startPause
{
    NSLog(@"暂停");
}

- (void)upDateUI
{
    HD_LiDa_PlayerTool *playTool = [HD_LiDa_PlayerTool sharedHD_LiDa_PlayerTool];
    self.currentTime.text = [NSString stringWithFormat:@"%02d:%02d",(int)playTool.currentTime/60,(int)playTool.currentTime%60];
    
    CGFloat time = playTool.totalTime- playTool.currentTime;
    self.allTime.text = [NSString stringWithFormat:@"%02d:%02d",(int)time/60,(int)time%60];
    self.progressSlider.value = playTool.currentTime/playTool.totalTime;
    [self.lrcTableView refreshWithIndex:(NSInteger)playTool.currentTime];
    
}


- (IBAction)backAction:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)lastMusicAction:(UIButton *)sender {
    [[HD_LiDa_PlayerTool sharedHD_LiDa_PlayerTool] prepareForPlayWithMusic:[[HD_LiDa_Playermanager sharedHD_LiDa_Playermanager] upMusicInfo]];
    [[HD_LiDa_PlayerTool sharedHD_LiDa_PlayerTool] play];
}
- (IBAction)nextMusicAction:(UIButton *)sender {
    [[HD_LiDa_PlayerTool sharedHD_LiDa_PlayerTool] prepareForPlayWithMusic:[[HD_LiDa_Playermanager sharedHD_LiDa_Playermanager] nextMusicInfo]];
    [[HD_LiDa_PlayerTool sharedHD_LiDa_PlayerTool] play];

}
- (IBAction)playCtrlAction:(UIButton *)sender {
    if (sender.selected) {
        [[HD_LiDa_PlayerTool sharedHD_LiDa_PlayerTool] play];
    }else{
        [[HD_LiDa_PlayerTool sharedHD_LiDa_PlayerTool] pause];
    }
}
- (IBAction)sliderTouchDown:(UISlider *)sender {
    [[HD_LiDa_PlayerTool sharedHD_LiDa_PlayerTool] pause];
}
- (IBAction)sliderTouchUpInside:(UISlider *)sender {
    HD_LiDa_PlayerTool *playTool = [HD_LiDa_PlayerTool sharedHD_LiDa_PlayerTool];
    playTool.currentTime = sender.value * playTool.totalTime;
    [[HD_LiDa_PlayerTool sharedHD_LiDa_PlayerTool] play];
}
- (IBAction)sliderValueChanged:(UISlider *)sender {
//    [[HD_LiDa_PlayerTool sharedHD_LiDa_PlayerTool] play];
}



- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
